package com.peng.leetcode.other;

import java.util.Arrays;

/**
 * Rrotate
 * 189. 旋转数组
 * <p>
 * 给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数。
 * <p>
 * 示例 1:
 * <p>
 * 输入: [1,2,3,4,5,6,7] 和 k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右旋转 1 步: [7,1,2,3,4,5,6]
 * 向右旋转 2 步: [6,7,1,2,3,4,5]
 * 向右旋转 3 步: [5,6,7,1,2,3,4]
 * <p>
 * 链接：https://leetcode-cn.com/problems/rotate-array
 *
 * @author: lupeng6
 * @create: 2021/1/8 11:03
 */
public class Rotate {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        int k = 3;
        new Rotate().rotate(nums, k);
        Arrays.stream(nums).forEach(System.out::println);
    }

    public void rotate(int[] nums, int k) {
        for (int i = 0; i < k; i++) {
            int prev = nums[0];
            for (int j = 1; j < nums.length; j++) {
                int temp = nums[j];
                nums[j] = prev;
                prev = temp;
            }
            nums[0] = prev;
        }
    }
}
